package top.superflow.flow.validate

import org.apache.commons.lang3.StringUtils
import top.superflow.inner.micro.flow.rule.RuleConfig

class RuleValidator extends ConfigValidator[RuleConfig]{
  
  def isValid(target : RuleConfig) : Boolean = {
    validateNull(target)
//    validateExpressionDependency(target)
    expressionNotBlank(target)
    
    true
  }
  
  
  private def validateNull(rule : RuleConfig){
    if(rule == null)
      throw new ValidationException("The rule can't be null!")
  }
  
  private def expressionNotBlank(rule : RuleConfig){
    if(StringUtils.isBlank(rule.getExpression))
      throw new ValidationException("The expression of the rule [" + rule.getKey + "] has been left blank, are you sure!")
  }
  
//  private def validateExpressionDependency(rule : RuleConfig) : Unit = {
//    val executableRule = ParserFactory.getInstance.getParser(rule).parse(rule).asInstanceOf[ExecutableRule]
//    
//    var variables = executableRule.getVariables
//    
//    //there is no need to check the dependencies
//    if(variables == null || variables.length > 0) {
//      return;
//    }
//    
//    val configuredEntities = InnerFlowConfiguration.getInstance.getEntities
//    if(configuredEntities.size == 0){
//      throw new RequestUnsupportedException("Please make sure there is curresponding entities configured for the rule " + rule.getKey)
//    }
//    
//    for(variableInExpression <- variables){
//      
//    }
//    
//  }
  
}